package com.yanqu.road.dao.impl.activity.qixi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.qixi.QixiBroadcastData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class QixiDaoImpl extends TempDao {
    public static String getQixiBroadcastSql = "select * from `t_c_qixi_broadcast` where `activityId`=? and `time` >= ?";

    public Map<Long, QixiBroadcastData> getQixiBroadcastDataMap(int activityId, long validTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, QixiBroadcastData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getQixiBroadcastSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, validTime);
            rs = call.executeQuery();
            while (rs.next()) {
                QixiBroadcastData data = parseQixiBroadcastData(rs);
                dataMap.put(data.getBroadcastId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String selectMaxBroadcastIdSql = "select max(`broadcastId`) from `t_c_qixi_broadcast` where `activityId`=?";
    public long getMaxBroadcastId(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectMaxBroadcastIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getLong(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return 0;
    }

    public QixiBroadcastData parseQixiBroadcastData(ResultSet rs) throws SQLException {
        QixiBroadcastData qixiBroadcastData = new QixiBroadcastData();
        qixiBroadcastData.setActivityId(rs.getInt("activityId"));
        qixiBroadcastData.setBroadcastId(rs.getLong("broadcastId"));
        qixiBroadcastData.setSendUserId(rs.getLong("sendUserId"));
        qixiBroadcastData.setReceiveUserId(rs.getLong("receiveUserId"));
        qixiBroadcastData.setSendUserBaseInfo(JSON.parseObject(rs.getString("sendUserBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        qixiBroadcastData.setReceiveUserBaseInfo(JSON.parseObject(rs.getString("receiveUserBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        qixiBroadcastData.setFlowerId(rs.getInt("flowerId"));
        qixiBroadcastData.setContent(rs.getString("content"));
        qixiBroadcastData.setTime(rs.getLong("time"));
        qixiBroadcastData.setNoneOption();
        return qixiBroadcastData;

    }

    public static String insertQixiBroadcastDataSql = "insert into `t_c_qixi_broadcast`(activityId,broadcastId,sendUserId,receiveUserId,sendUserBaseInfo,receiveUserBaseInfo,flowerId,content,time) values (?,?,?,?,?,?,?,?,?)";
    public void saveQixiBroadcastDataList(List<QixiBroadcastData> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCrossDb());

        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (QixiBroadcastData qixiBroadcastData : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, qixiBroadcastData.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, qixiBroadcastData.getBroadcastId()));
            para.put(i++, new DbParameter(Types.BIGINT, qixiBroadcastData.getSendUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, qixiBroadcastData.getReceiveUserId()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(qixiBroadcastData.getSendUserBaseInfo())));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(qixiBroadcastData.getReceiveUserBaseInfo())));
            para.put(i++, new DbParameter(Types.INTEGER, qixiBroadcastData.getFlowerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, qixiBroadcastData.getContent()));
            para.put(i++, new DbParameter(Types.BIGINT, qixiBroadcastData.getTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(insertQixiBroadcastDataSql, mapList) > -1) {
            result = true;
        }
    }
}
